
var $table = $('#addTo-index-goods');
//表示蜜雅阁
var selectedGroupType =1;
$(function () {
    //banner图图片地址
    initFileInput("file_upload", urlcore + "/api/attachment/upload", "#picUrl");


    //创建待分组商品列表
    initUngroupedTable(selectedGroupType);
});




// 初始化当前产品线待分组商品bootstrap-table
function initUngroupedTable(selectedGroupType) {
    // 初始化bootstrap-table
    var url=urlcore + "/api/mygPainting/getNotGroupedGoodsList";
    //init table params
    var params = {
        goodsType: selectedGroupType
    };
    initGroupTable(url,params);

}


//添加商品
function add(){
	var selection = $table.bootstrapTable('getSelections');
		//图片地址
    var picUrl = "";
    $("#picUrl img").each(function(index,item){
        if(index==0){
            picUrl += $(this).attr("src");
        }
	});
	
    if (selection.length == 0) {
        alert("请先选择商品");
        return;
    }

    var Goods = [];
    for (var item of selection) {
        Goods.push({
            goodsId: item.id,
            picUrl:picUrl,
        })
    }
	
	    $.ajax({
        type: "POST",
        url: urlcore + "/api/mygPainting/addBatch",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(Goods),
        success: function (data) {
            alert("添加成功");
            //$table.bootstrapTable('refresh');
            location.reload();
        },
        error: function () {
            alert("失败");
        }
    });

}


//初始化表格，有管理已有商品和添加分组商品两类表格
function initGroupTable(url,params){
    $table.bootstrapTable({
        columns: [{
            field: "state",
            checkbox: true,
            title: "ID"
        }, {
            field: "id",
            title: "ID"
        }, {
            field: "goodsName",
            title: "商品名称"
        }, {
            field: "goodsUrl",
            title: "商品图片",
            align: "center",
            width:"100px",
            formatter: function (value, row, index) {
                return '<img style="width: 50px;height: auto" src="' + value + '" >';
            }
        }, {
            field: "mPrice",
            title: "标价"
        }],
        method: "get",
        url:url ,
        dataType: "json",
        queryParams: params,
        //返回数据预处理
        responseHandler: function (cbdata) {
            var res = cbdata.data;
            return res;
        },
        toolbar:'#addGroupToolbar',
        pagination: true,//启用分页条
        sidePagination: "client",
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,
        sortable: true,//是否启用排序
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,
        // searchText:"",//初始搜索文本

    });
    
    
}